﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using viNews.Classes;
using viNews.Controllers;

public partial class Admin_Pages_viNews_Categories_CategoryDropDownList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string valueFormat = "{0},{1}";
            List<Category> parentcategories = CategoryController.GetParentCategories();
            foreach (Category parent in parentcategories)
            {
                {
                    string value = string.Format(valueFormat, parent.CategoryId, 0);
                    ListItem li = new ListItem(parent.Name, value);
                    CATEGORY.Items.Add(li);
                }
                List<Category> subcategories = CategoryController.GetCategories(parent.CategoryId);
                foreach (Category subCategory in subcategories)
                {
                    string value = string.Format(valueFormat, parent.CategoryId, subCategory.CategoryId);
                    ListItem li = new ListItem(parent.Name + " / " + subCategory.Name, value);
                    CATEGORY.Items.Add(li);
                }
            }
            if (ViewState["SelectionData"] != null)
                CATEGORY.SelectedValue = (string)ViewState["SelectionData"];
        }
    }

    /// <summary>
    /// Gets or sets the selected categoryid 
    /// </summary>
    public int CategoryId
    {
        get
        {
            string temp = CATEGORY.SelectedValue;
            if (string.IsNullOrEmpty(temp))
                return -1;
            string[] str = temp.Split(',');
            if (Convert.ToInt32(str[1]) == 0)
                return Convert.ToInt32(str[0]); // Parent Only
            else
                return Convert.ToInt32(str[1]);// Child Only
        }
        set
        {
            string valueFormat = "{0},{1}";
            int ParentId = CategoryController.GetParentCategoryId(value);
            if (ParentId == 0)
                ViewState["SelectionData"] = string.Format(valueFormat, value, 0);
            else
                ViewState["SelectionData"] = string.Format(valueFormat, ParentId, value);
        }
    }

    /// <summary>
    /// Gets the current selected category name
    /// </summary>
    public string CategoryName
    {
        get
        {
            string temp = CATEGORY.SelectedValue;
            if (string.IsNullOrEmpty(temp))
                return null;
            string[] str = temp.Split(',');
            if (Convert.ToInt32(str[1]) == 0)
                return CategoryController.GetCategory(Convert.ToInt32(str[0])).Name;
            else
                return CategoryController.GetCategory(Convert.ToInt32(str[1])).Name;
        }
    }

    /// <summary>
    /// Gets the control itself
    /// </summary>
    public DropDownList Control
    {
        get { return CATEGORY; }
    }

    /// <summary>
    /// Gets or Sets the control Css Class
    /// </summary>
    public string CssClass
    {
        get { return CATEGORY.CssClass; }
        set { CATEGORY.CssClass = value; }
    }

    /// <summary>
    /// Gets or Sets the control Width
    /// </summary>
    public Unit Width
    {
        get { return CATEGORY.Width; }
        set { CATEGORY.Width = value; }
    }    
}
